package Myfun;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-06-23
 * Time: 16:18
 */
//创建一个简单的线程
public class MyThreadPollBasicEdition {
    //创建一个任务队列
    public BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
    //初始化线程池
    public MyThreadPollBasicEdition(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()-> {
                    try {
                        while(true) {
                            Runnable runnable = queue.take();
                            runnable.run();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
            });
            t.start();
        }
    }
    //提供submit方法
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public static void main(String[] args) throws InterruptedException {
        MyThreadPollBasicEdition myThreadPollBasicEdition = new MyThreadPollBasicEdition(10);
        for (int i = 0; i < 50000; i++) {
            int id = i;
            myThreadPollBasicEdition.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello " + id + ", " + Thread.currentThread().getName());
                }
            });
        }
    }
}
